<?php

namespace App\Http\Controllers\Common;

use Image;
use File;
use Auth;
use Illuminate\Http\Request;

use App\Models\User;
use App\Models\Images;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    
    /**
     *
     * 上传头像方法
     * 
     * @param  \Illuminate\Http\Request  $request
     *
     * @return boolean
     */
    public function storeheadportrait(Request $request)
    {
        
        $info = array();
        $request = $request->all();
        
        $img_base64 = base64_decode(str_replace('data:image/jpeg;base64,', '', $request['imgdata']));
        if(empty($img_base64)){
            $info['status'] = 10;
            $info['msg'] = '获取图像失败!';
            return json_encode($info);
        }
        
        $img_size = strlen($img_base64)/1024/1024; // 单位是M
        if((float)config('url.max_image_size') <= $img_size){
            $info['status'] = 11;
            $info['msg'] = '图像太大，请重新上传!';
            return json_encode($info);
        }
        
        $file_path = config('url.images_path').config('url.image_type_dir.headportrait');
        $file_name = config('url.imges_name').'.jpg';
        if(!File::exists($file_path)){
            File::makeDirectory($file_path,  $mode = 0777, $recursive = false);
        }
        
        // 保存图片到服务器
        File::put($file_path.$file_name, $img_base64);
        
        if(!File::isFile($file_path.$file_name)){
            $info['status'] = 12;
            $info['msg'] = '图像不存在，请重新上传!';
            return json_encode($info);
        }
        
        $img_info = exif_read_data($file_path.$file_name);
        
        $data = array();
        $data['user_id'] = (int)Auth::user()->id;
        $data['filename'] = $file_name;
        $data['filepath'] = config('url.image_type_dir.headportrait');
        $data['filesize'] = $img_info['FileSize'];
        $data['filewidth'] = $img_info['COMPUTED']['Width'];
        $data['fileheight'] = $img_info['COMPUTED']['Height'];
        
        $images_model = new Images();
        $images = $images_model->create($data);
        if(!$images){
            $info['status'] = 13;
            $info['msg'] = '图像保存，请重新上传!';
            return json_encode($info);
        }
        
        $user_model = new User();
        $user = $user_model::where('id',(int)$images->user_id)->first();
        $user->headportrait_id = $images->id;
        $user = $user->save();
        if(!$user){
            $info['status'] = 14;
            $info['msg'] = '图像保存用户表失败，请重新上传!';
            return json_encode($info);
        }
        
        $info['status'] = 1;
        $info['msg'] = '上传成功!';
        return json_encode($info);
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
